/****************************************
* 指針
*  Created by zephyrus on 2024/6/27.
*****************************************/
#include "stdio.h"
#include "stdint.h"
#include "string.h"
//#ifdef _WIN_32
//#include <Windows.h>
//#elif __linux
//
//#endif

//typedef struct stu10 stu10;
//volatile uint8_t DetectorBuffCnt;

//struct stu10{
//    char sex;
//    int length;
//    char name[10];
//}__attribute__((aligned(1)));
//
//struct stu10 stu101;

#define SOFT_SYS_RST_ENABLE 1
#define  upgrade 0x20
int add(int x, int y)
{
    return x + y;
}

void test100(uint8_t *bufStr);


int main()
{
//    int a=5;
//    int *pa=&a;
//    printf("%d \n",a);
//    printf("%p \n",*pa);
//    printf("%p \n",pa);
//
//    *pa=1;
//    printf("%p \n",pa);
//    printf("%d \n",*pa);
    //printf("%d",DetectorBuffCnt);
//    uint8_t showBuffer[5];
//    showBuffer[1]|=0x80;
//    showBuffer[3]|=0x80;
//    showBuffer[1]|=0x5f;
//    printf("%u",showBuffer[1]);

    //printf("%d", sizeof(stu101));

//    HMODULE h=NULL;
//    h= LoadLibrary("libdll_test.dll");
//    if(h==NULL){
//        printf("load dll error!.\n");
//        return -1;
//    }
//    typedef double (*GetArea)(float);
//    typedef double (*GetLength)(float);
//    GetArea getArea;
//    GetLength getLength;
//    getArea=(GetArea) GetProcAddress(h,"GetCircleArea");
//    getLength=(GetLength)GetProcAddress(h,"GetCircleArea");
//    float r=10;
//    printf("圆的面积:%f\n",getArea(r));
//    printf("圆的周长:%f\n",getLength(r));

    int a = 20;
    int *pa = &a;
    printf("%d\n", *pa);


    int num[10] = {0, 1, 2, 3, 4, 5};
    int *p = num;
    int *ps = "0x5ffe10";
    char *pc = "0x5ffe10";
    printf("value=%d\n", *pc);
    printf("value=%d \n", *ps);
    printf("%d \n", num[1]);
    printf("%d \n", *(p + 1));
    printf("%d \n", *(num + 1));
    printf("%d \n", *p);
    printf("%d \n", &num);
    printf("get pointer's address: %d\n", &p); //指针是变量，定义变量就有地址
    printf("get pointer's store address: %d\n", p); //指针存储的是指向指针指向某个变量的地址，如果是数组，则为首地址
    printf("get pointer's store address: %d\n", *p); //指针存储的是指向指针指向某个变量的地址 *取地址的值


    for (int i = 0; i < 10; i++)
    {
        printf("%x ", p + i);
    }
    printf("\n");
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", *(p + i));
    }

    printf("\n");

    const int b = 20; //不能变
    int *const pcn = &b;
    int *const pcn1 = pcn;
    //b=30;
    printf("pcn=%d b=%d pcn address=%d pcn1=%d\n", pcn, &b, &pcn, *pcn1);

    int *p1 = &b;
    char *p2 = p1;
    printf("p2=%d", *p2);
    printf("\n");

    void *p3 = p2;
    int *p4 = p3;
    //printf("p3=%d",*p3); //不能用
    printf("p4=%d \n", *p4);


    int *p5 = &a;

    char *p6 = (char *) p1;

    printf("p6=%d \n", *p6);


    char *name = "zhangsan";
    printf("name=%d \n", &name);

    char *pstr = name;
    while (*pstr != '\0')
    {
        printf("%c", *pstr);
        pstr = pstr + 1;
    }

    printf("\n");
    int z[3] = {1, 2, 3};
    void *pz = z;
    printf("1:pz=0x%x \n", pz);
    pz = pz + 1;
    printf("2:pz=0x%x \n", pz);

    int (*pzz)[3] = z;
    printf("1:pzz=0x%x \n", z);
    printf("2:pzz=0x%x \n", &z);
    printf("3:pzz=0x%x \n", pzz);
    printf("pzzaddr0x%x \n", &pzz);
    //pzz=pzz+1;
    printf("pzzaddr0x%x \n", *(pzz + 1));

    //指针数组
    int *pzzz[3] = {&z, &z[1], &z[2]};
    printf("0x%x,0x%x,0x%x \n", *pzzz, *(pzzz + 1), *(pzzz + 2));

    int (*pfun)(int, int);
    pfun = add;
    printf("%d+%d=%d\n", 1, 2, pfun(1, 2));

    int parr[] = {1, 2, 3, 4, 5, 6};
    int size = sizeof(parr) / sizeof(parr[0]); //计算内存带宽，如果int占4节
    int size1 = sizeof(parr);
    printf("%d \n", size1);
    printf("%d \n", size);
    unsigned long long l4 = 10;
    printf("%d \n", sizeof(l4));

    printf("%d \n", SOFT_SYS_RST_ENABLE);


    uint8_t bufStr[] = {41, 57, 02, 00, 24, 36, 02, 05, 0xEE, 0x22, 0x99,0x00};
    test100(bufStr);
    printf("数组长度:%d \n",sizeof(bufStr)/sizeof(bufStr[0]));
    int astr[]={1,2,3,4,5};
    printf("astr数组长度:%d \n",sizeof(astr)/sizeof(astr[0]));
    printf("数组:0x%.2x,\n",bufStr[12]);
    printf("数据最后一位是否相等:%d\n",bufStr[12]==0x01);
    printf("数组长度:%d\n",sizeof(bufStr)/sizeof(bufStr[0]));

    int bufInt[]={1,2,3,4,5};
    uint8_t bufUInt[6]={61,66,36,46,56,255};
    bufUInt[6]='\0';
    printf("数组:%d\n",bufInt[5]);
    //printf("数组uint:%d\n",bufUInt[4]);
//    for(int i=0;i<sizeof(bufUInt)/sizeof(bufUInt[0]);++i){
//        printf("数组uint: %d ",bufUInt[i]);
//    }
    for(int i=0;i<7;++i){
        printf("数组uint: %d ",bufUInt[i]);
    }
//    printf("\n");
//    uint8_t *pbufUint=(bufUInt+6);
//    printf("%d \n",*(pbufUint+1));
//    if(*pbufUint==0x01){
//        printf("test success \n");
//    }
//
//    int aaaa=100;
//    int bbbb=80;
//    printf("%d \n",aaaa|=bbbb);
//
//    uint8_t ipinfo1;
//    ipinfo1|=upgrade;
//    printf("%d\n",ipinfo1); //32 0x20=16*2+0*1

    return 0;
}

void test100(uint8_t *bufStr)
{
    uint8_t *bfstr = (bufStr + 12);

    printf("指针:0x%.2x\n",*bfstr);



    printf("Test null=null: %d \n",*bfstr==0);

    if(0==0){
        printf("test 0==0\n");
    }
    printf("%x",*bfstr);

    if(bfstr!=0){
        printf("test %d\n",bfstr==0x00);
    }
    printf("%d \n", bfstr);
    printf("0x%.2x \n", *(bfstr + 1));
}

void printf_buf_hex(uint8_t *bufPtr, uint8_t len)
{
    uint8_t i, pvalue;
    for (i = 0; i < len;)
    {
        pvalue=bufPtr[i];
    }
}